package com.hp.android.logmonitor.service;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import android.app.IntentService;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.util.Log;

public class LogMonitorService extends Service {

	private final String TAG = "LogMonitorService";
	private Process mLogProcess = null;
	private Messenger messenger = null;
	private IBinder mBinder = new LocalBinder();

	public class LocalBinder extends Binder {
		public LogMonitorService getService() {
			// Return this instance of LocalService so clients can call public
			// methods
			return LogMonitorService.this;
		}
	}

	@Override
	public void onCreate() {
		super.onCreate();
		mThread.start();
	}

	public LogMonitorService() {
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {

		return START_NOT_STICKY;
	}

	@Override
	public IBinder onBind(Intent intent) {
		messenger = (Messenger) intent.getExtras()
				.get(LogMonitor.LOG_MESSGENER);

		return mBinder;
	}

	@Override
	public boolean onUnbind(Intent intent) {
		return super.onUnbind(intent);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	Thread mThread = new Thread("LogMonitor thread") {
		public void run() {
			parseLogFile();
		};
	};

	public void parseLogFile() {
		String fileName = "logcat_file";
		FileOutputStream fos = null;
		try {

			fos = openFileOutput(fileName, Context.MODE_PRIVATE);

			// now that we have a remote connection let's start up the logcat.
			mLogProcess = Runtime.getRuntime().exec("/system/bin/logcat -d");

			BufferedReader bufReader = new BufferedReader(
					new InputStreamReader(mLogProcess.getInputStream()), 8092);

			String buf;
			String newline = "\n";

			int i = 0;
			StringBuffer sb = new StringBuffer();
			while (bufReader.readLine() != null) {
				
				buf = bufReader.readLine();
				if (mLogProcess == null)
					break;
				fos.write(buf.getBytes());
				fos.write(newline.getBytes());
				sb.append(new String(buf.getBytes()));
				sb.append(new String(newline.getBytes()));
				String logString = sb.toString();
				i++;
				if (i == 10) {
					i = 0;
					sb = new StringBuffer();
					Message msg = Message.obtain();
					Bundle extras = new Bundle();
					extras.putString(LogMonitor.MSG_CALLBACK, logString);
					msg.setData(extras);
					if (messenger != null) {
						try {
							messenger.send(msg);
						} catch (android.os.RemoteException e1) {
							Log.w(getClass().getName(),
									"Exception sending message", e1);
						}
					}
				}
			}

			if (bufReader != null)
				bufReader.close();
			mLogProcess.destroy();
			mLogProcess = null;

		} catch (Exception e) {
			Log.w(getClass().getName(), "Exception sending message", e);
		} finally {
			try {
				fos.close();
			} catch (IOException e) {

				e.printStackTrace();
			}
		}

	}

}
